//
//  HSYCorporateInformationViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/6.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYCorporateInformationViewController.h"
#import "HSYAccountInformationViewController.h"
#import "UIImagePickerController+Block.h"
#import "BQActivityView.h"
#import "UIImage+Tools.h"
#import "BQActivityView.h"
#import "HSYImproveStoreContactInformationRequest.h"
#import "NSString+Validation.h"

@interface HSYCorporateInformationViewController ()

@property (weak, nonatomic) IBOutlet UITextField *corporateNameTextField; //联系人姓名
@property (weak, nonatomic) IBOutlet UITextField *legalPersonContactPhone; //联系人手机号

@property (weak, nonatomic) IBOutlet UIImageView *legalPersonIDCardFrontalPic; //身份证正面
@property (weak, nonatomic) IBOutlet UIImageView *legalPersonIDCardbackPic; //身份证反面

@property (weak, nonatomic) IBOutlet UIButton *legalPersonIDCardFrontalPicBtn;
@property (weak, nonatomic) IBOutlet UIButton *legalPersonIDCardbackPicBtn;

@end

@implementation HSYCorporateInformationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"填写店铺资料" forState:UIControlStateNormal];
    
    [self fillbackIfNeed];
}

- (void)fillbackIfNeed{
    self.corporateNameTextField.text = [HSYUserCenter instatce].appBizShop.contactName;//联系人
    self.legalPersonContactPhone.text = [HSYUserCenter instatce].appBizShop.contactCellphone; //联系人电话
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handleGoBackEvent:(UIButton *)sender{
    if (self.navigationController.childViewControllers.count <= 1) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:NO];
    }
}


- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}


- (IBAction)nextStepBtnClick:(id)sender {
    [self.view endEditing:YES];

    if (![self checkDataIntegrity]) return;
    
    HSYImproveStoreContactInformationRequest *task = [[HSYImproveStoreContactInformationRequest alloc] init];
    task.shopId = [HSYUserCenter instatce].appBizShop.shopID;
    task.uid = [HSYUserCenter instatce].appAuUser.uid;
    task.contactName = self.corporateNameTextField.text;
    task.contactCellphone = self.legalPersonContactPhone.text;
    
    task.fileA = UIImageJPEGRepresentation(self.legalPersonIDCardFrontalPic.image, 0.1);
    task.fileB = UIImageJPEGRepresentation(self.legalPersonIDCardbackPic.image, 0.1);
    
    __weak typeof(self)weakSelf = self;
    [BQActivityView showActiviTy];

    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];
        
        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            //保持步骤信息
            [HSYUserCenter instatce].appAuUser.auStep = 3;
            [[HSYUserCenter instatce] saveUserModelToDocuments];
            
            //去完善账户信息
            [weakSelf pushToSetAccountInformationVC];

        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}


-(BOOL)checkDataIntegrity{
    if (!self.corporateNameTextField.text.length) {
        [self showMessage:@"请填写联系人姓名"];
        return NO;
    }else if (![self.corporateNameTextField.text isValidPersonName]){
        [self showMessage:@"请输入正确联系人名称"];
        return NO;
    }
    
    if (!self.legalPersonContactPhone.text.length) {
        [self showMessage:@"请填写联系电话"];
        return NO;
    }else if (![self.legalPersonContactPhone.text isValidPhone]){
        [self showMessage:@"手机号错误，请重新输入"];
        return NO;
    }
    
    
    if (!self.legalPersonIDCardFrontalPic.image) {
        [self showMessage:@"请拍摄法人正面照"];
        return NO;
    }
    
    if (!self.legalPersonIDCardbackPic.image) {
        [self showMessage:@"请拍摄法人背面照"];
        return NO;
    }
    
    return YES;
}

- (IBAction)legalPersonIDCardFrontalPicBtnClick:(UIButton *)sender {
    
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.legalPersonIDCardFrontalPicBtn setImage:nil forState:UIControlStateNormal];
        wSelf.legalPersonIDCardFrontalPic.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
    
}

- (IBAction)legalPersonIDCardbackPicBtnClick:(UIButton *)sender {
    
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        [wSelf.legalPersonIDCardbackPicBtn setImage:nil forState:UIControlStateNormal];
        wSelf.legalPersonIDCardbackPic.image = image;
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
    
}

#pragma mark - 完善账户信息 -

- (void)pushToSetAccountInformationVC{
    [HSYGoToAuditUtil goAuditFromViewController:self push:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
